package com.sea.base.dialog

import android.view.View
import android.widget.FrameLayout
import androidx.annotation.StyleRes
import androidx.viewbinding.ViewBinding
import com.sea.base.R
import com.sea.base.ui.IUIContext
import com.sea.base.utils.ViewBindingHelper

abstract class BaseViewBindingDialog<T : ViewBinding>(ui: com.sea.base.ui.IUIContext, @StyleRes themeResId: Int = R.style.CustomDialog) : BaseDialog(ui, themeResId) {

    companion object {
        /**
         * 在mvvm里，一般的Dialog就是普通的v层，所以降级到activity里去
         */
        inline fun <T : ViewBinding> create(
            ui: com.sea.base.ui.IUIContext,
            isShow: Boolean = false,
            crossinline onDialogCreate: BaseViewBindingDialog<T>.() -> Unit
        ) =
            object : BaseViewBindingDialog<T>(ui) {
                override fun onCreate() {
                    onDialogCreate()
                }
            }
                .also { if (isShow) it.show() }
    }

    lateinit var vb: T
        private set

    override fun createRootView(inflateLayout: FrameLayout): View {
        vb = ViewBindingHelper.getViewBindingInstance(
            this@BaseViewBindingDialog,
            layoutInflater,
            inflateLayout,
            false
        )
        return vb.root
    }
}